unit notebook_property;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, SQLDB, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, ButtonPanel, RTTICtrls;

type

  { Tform_notebook_property }

  Tform_notebook_property = class(TForm)
    ButtonPanel1: TButtonPanel;
    edit_name: TEdit;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    sql_query_notebook: TSQLQuery;
    tv_notebook: TTreeView;
    procedure CancelButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
  private
    FDestNotebookId: integer;
    procedure RefreshNotebook;
    procedure UpdateNotebook(notebook_id: integer; notebook_name: string;
      parent_notebook: integer);
    procedure SetDestNotebookId(const Value: integer);
  public
    property DestNotebookId: integer read FDestNotebookId write SetDestNotebookId;
  end;

var
  form_notebook_property: Tform_notebook_property;

implementation

uses
  main;
  {$R *.lfm}

  { Tform_notebook_property }

procedure Tform_notebook_property.SetDestNotebookId(const Value: integer);
begin
  FDestNotebookId := Value;
end;

procedure Tform_notebook_property.RefreshNotebook;
var
  sql: string;
  ANode, ParentNode: TTreeNode;
begin
  sql := 'WITH RecursiveNotebook AS (' + ' SELECT id, parent, name ' +
    ' FROM notebook ' + ' WHERE parent IS NULL ' + ' UNION ALL ' +
    ' SELECT c.id, c.parent, c.name ' + ' FROM notebook c ' +
    ' INNER JOIN RecursiveNotebook rc ON c.parent = rc.id ' +
    ') SELECT * FROM RecursiveNotebook;';
  sql_query_notebook.Close;
  sql_query_notebook.SQL.Text := sql;
  sql_query_notebook.Open;

  //tv_notebook.Items.Clear;
  while not sql_query_notebook.EOF do
  begin
    if sql_query_notebook.FieldByName('parent').IsNull then
      ParentNode := nil
    else
      ParentNode := tv_notebook.Items.FindNodeWithData(
        Pointer(sql_query_notebook.FieldByName('parent').AsInteger));

    ANode := tv_notebook.Items.AddChild(ParentNode,
      sql_query_notebook.FieldByName('name').AsString);
    ANode.Data := Pointer(sql_query_notebook.FieldByName('id').AsInteger);
    ANode.ImageIndex := 0;
    sql_query_notebook.Next;
  end;
end;

procedure Tform_notebook_property.UpdateNotebook(notebook_id: integer;
  notebook_name: string; parent_notebook: integer);
var
  sql_query: TSQLQuery;
  sql: string;
begin
  sql_query := TSQLQuery.Create(nil);
  try
    sql_query.SQLConnection := form_main.sqlite3_connection;
    if parent_notebook >= 0 then
        sql := Format('update notebook set name = "%s" , parent = %d where id = %d;',
          [notebook_name, parent_notebook, notebook_id])
    else
        sql := Format('update notebook set name = "%s" , parent = null where id = %d;',
          [notebook_name, notebook_id]);
    sql_query.SQL.Text := sql;
    sql_query.ExecSQL;
    form_main.sql_transaction.Commit;
  finally
    sql_query.Close;
    sql_query.Free;
  end;
end;

procedure Tform_notebook_property.OKButtonClick(Sender: TObject);
var
  notebook_name: string;
  parent_id: integer;
begin
  notebook_name := edit_name.Text;
  if tv_notebook.Selected.Data <> nil then
    parent_id := integer(tv_notebook.Selected.Data)
  else
    parent_id := -1;

  self.UpdateNotebook(self.DestNotebookId, notebook_name, parent_id);
  ModalResult := mrOk;
end;

procedure Tform_notebook_property.CancelButtonClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure Tform_notebook_property.FormShow(Sender: TObject);
begin
  tv_notebook.Items.AddChild(nil, '空');
  self.RefreshNotebook;
end;

end.
